package com.bwie.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import utils.MD5keyBean;
import utils.PageUtil;

import com.bwie.dto.Company;
import com.bwie.dto.Person;
import com.bwie.dto.Position;
import com.bwie.dto.User;
import com.bwie.service.WorkService;
@Controller
@RequestMapping("/work")
public class WorkController  {

	@Autowired
	private WorkService service;
	@RequestMapping("/list.controller")
	public String list(HttpServletRequest request,Person person){
		/*List<Person> list = service.getPersonList();
		request.setAttribute("list", list);*/
		List<Company> listCompany = service.getCompanyList();
		System.out.println(456);
		System.out.println(789);
		System.out.println(1111);
		System.out.println(5555);
		request.setAttribute("listCompanyan", listCompany);
		List<Position> listPosition = service.getPositionList();
		request.setAttribute("listPosition", listPosition);
		/*
		 * 
		 * (HttpServletRequest request, 
		 * String url, 
		 * Integer pageSize,
		 * List<?> list,
		 * Integer listCount, 
		 * Integer page) 
		 *  
		 */
		Integer page = request.getParameter("page")==null?1:Integer.parseInt(request.getParameter("page"));
		String url= "/work/list.controller";
		Integer pageSize =2;
		Integer startIndex = (page-1)*pageSize;
		Map<Object,Object> map = new HashMap<Object, Object>();
		map.put("startIndex", startIndex);
		map.put("pageSize", pageSize);
		map.put("name", person.getName());
		map.put("age", person.getAge());
		map.put("hobby", person.getHobby());
		List<Person> list = service.getPersonList(map);
		Integer listCount = service.getPersonCount(map);
		PageUtil.page(request, url, pageSize, list, listCount, page, map);
		return "list";
	}
	//toadd.controller
	@RequestMapping("/toadd.controller")
	public String toadd(){
		
		return "add";
	}
	@ResponseBody
	@RequestMapping("/findCompanyList.controller")
	public List<Company> findCompanyList(){
		List<Company> listCompany = service.getCompanyList();
		return listCompany;
	}
	@ResponseBody
	@RequestMapping("/findPositionList.controller")
	public List<Position> findPositionList(){
		List<Position> listPosition = service.getPositionList();
		return listPosition;
	}
	//add.controller
	@RequestMapping("/add.controller")
	public String add(Person person){
		service.add(person);
		return "redirect:list.controller";
	}
	@RequestMapping("/toregister.controller")
	public String toregister(){
		
		return "register";
	}
	//register.controller
	@RequestMapping("/register.controller")
	public String register(User user,HttpServletRequest request){
		MD5keyBean md = new MD5keyBean();
		String jmResult = md.getJmResult(user.getPassword());
		User user2 = new User(null, user.getUsername(), jmResult);
		Integer i=service.add(user2);
		if(i!=0){
			
			return "login";
		}	
		else
		{
			request.setAttribute("error", "ע��ʧ��");
			return "register";
		}
		
	}
	//toupdate.controller
	@RequestMapping("/toupdate.controller")
	public String toupdate(){
		return "update";
	}
	//findObjectById.controller
	@ResponseBody
	@RequestMapping("/findObjectById.controller")
	public Person findObjectById(Person person){
		Person person2=service.findObjectById(person);
		return person2;
	}
	@RequestMapping("/update.controller")
	public String update(HttpServletRequest request,Person person){
		service.update(person);
		return "redirect:list.controller";
	}
	@RequestMapping("/toaddall.controller")
	public String toaddall(){
		return "addall";
	}
	@RequestMapping("/addall.controller")
	public  String addall(Person person){
		for (int i = 0; i < 100; i++) {
			service.addall(person);
		}
	
		return "redirect:list.controller";
	}
	@RequestMapping("/tologin.controller")
	public  String tologin(){
		return "login";
	}
	@RequestMapping("/login.controller")
	public  String login(User user,HttpServletRequest request){
		MD5keyBean md = new MD5keyBean();
		String jmResult = md.getJmResult(user.getPassword());
		User user3 = new User(null, user.getUsername(), jmResult);
		User i=service.getOneByUser(user3);
		if(i!=null){
			request.getSession().setAttribute("hehe", "����");
			request.getSession().setAttribute("hehe", "��֤");
			request.getSession().setAttribute("hehe", "2");
			request.getSession().setAttribute("xixi", 3);
			request.setAttribute("hu", "request");
			return "redirect:list.controller";
		}else {
			return "login";
		}
	}
	/*****************************/
	public WorkService getService() {
		return service;
	}
	public void setService(WorkService service) {
		this.service = service;
	}
	
}
